package com.iot.controller;


import com.iot.base.Basic;
import com.iot.domain.Album;
import com.iot.service.FastDfsService;
import com.iot.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/*获取所有的荣誉照片*/
@Controller
public class HonerController {


    @Autowired
    RedisService  redisService;

    @Autowired
    FastDfsService fastDfsService;


    /*上传照片到指定的相册*/
    @RequestMapping("/uploadHoners")
    public String upLoadHoner(MultipartHttpServletRequest request)  {

        try {

            //获取请求中的文件
            List<MultipartFile> files = request.getFiles("honerPictures");

            //交给业务层处理
            fastDfsService.uploadFileByName(Basic.HONERS,files);
            return "forward:getAllHoner";

        } catch (Exception e) {
            e.printStackTrace();

        }
        return "honer";
    }


    /*进入荣誉墙*/
    @RequestMapping("/getAllHoner")
    public ModelAndView  getAllHoner() throws IOException {

        /*获取所有的荣誉的照片*/
        Album pictureByAlbumName = redisService.findPictureByAlbumName(Basic.HONERS);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("showhoners");
        modelAndView.addObject("honerPictures",pictureByAlbumName.getPictures());

        return modelAndView;

    }

}
